<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上级分类" prop="pid">
        <el-select v-model="queryParams.pid" placeholder="全部" @change="handleQuery">
          <el-option
            v-for="item in ClassifyOneList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="分类等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="全部" @change="handleQuery">
          <el-option
            v-for="dict in dict.type.classify_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" @change="handleQuery">
          <el-option
            v-for="dict in dict.type.good_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['video:DClassify:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleEnableBach1"
          v-hasPermi="['video:DClassify:edit']"
        >批量启用/禁用分类</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['video:DClassify:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['video:DClassify:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['video:DClassify:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DClassifyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="分类ID" align="center" prop="id" />-->
      <el-table-column label="分类名称" align="center" prop="name" />
      <el-table-column label="序号" align="center" prop="sort" />
<!--      <el-table-column label="分类图片" align="center" prop="imageUrl" />-->
      <el-table-column prop="imageUrl" label="分类图片" width="90"  align="center">
        <template slot-scope="scope">
          　　　    <img :src="scope.row.imageUrl" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column label="分类等级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.classify_level" :value="scope.row.level"/>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '1'">启用</span>
          <span v-if="scope.row.status == '2'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:DClassify:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['video:DClassify:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="分类等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择" @change="handleChange">
            <el-option
              v-for="dict in dict.type.classify_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="一级分类" prop="pid" v-if="is_show">
          <el-select v-model="form.pid" placeholder="请选择">
            <el-option
              v-for="item in ClassifyOneList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="分类图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.good_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="批量启用/禁用" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form1" :rules="rules1" label-width="100px">

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form1.status">
            <el-radio
              v-for="dict in dict.type.good_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDClassify,
  getDClassify,
  delDClassify,
  addDClassify,
  updateDClassify,
  listDClassifyVo, updateDClasStatus
} from "@/api/video/DClassify";
import {updateDGoodsBach} from "@/api/video/DGoods";

export default {
  name: "DClassify",
  dicts: ['good_status','classify_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类表格数据
      DClassifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      //是否显示批量启用弹出层
      open1:false,

      //是否显示批量禁用用弹出层
      open2:false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sort: null,
        status: null,
        pid: null,
        level: null,
      },
      is_show: false,
      ClassifyOneList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 表单参数
      form1: {},
      // 表单校验
      rules1: {
      },

    };
  },
  created() {
    this.getList();
    this.getClassifyList();
  },
  methods: {
    //多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /**查询一级分类集合**/
    getClassifyList() {
      listDClassifyVo().then(response => {
        this.ClassifyOneList = response.rows;
      });
    },
    /** 查询分类列表 */
    getList() {
      this.loading = true;
      listDClassify(this.queryParams).then(response => {
        this.DClassifyList = response.rows;
        this.DClassifyList.forEach((item,index)=> {
          if(item.imageUrl!=null) {
            if (item.imageUrl.includes('iyuanchuang.cn')) {
              item.imageUrl = item.imageUrl.replace("iyuanchuang.cn", "iyuanchuang.com.cn")
            }
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    //批量启用禁用 取消按钮
    cancel1() {
      this.open1 = false;
    },
    handleEnableBach1(row) {
      this.form1.id=row.id || this.ids
      this.open1 = true;
    },
    //批量启用禁用提交
    submitForm1() {
      if (this.form1.id != null) {
        updateDClasStatus(this.form1).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open1 = false;
          this.getList();
        });
      }
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sort: null,
        imageUrl: null,
        pid: null,
        level: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDClassify(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分类";
        //二级分类需要展示
        this.handleChange();

      });
    },
    //选择分类等级
    handleChange(){
      const level=this.form.level;
      console.log("level："+level)
      if(level == 2){
        this.is_show= true;
      }else{
        this.is_show= false;
      }

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('删除分类编号"' + ids + '"的数据同时会删除分类下商品数据!').then(function() {
        return delDClassify(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/DClassify/export', {
        ...this.queryParams
      }, `DClassify_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
